var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all) __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if ((from && typeof from === 'object') || typeof from === 'function') {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, {
          get: () => from[key],
          enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable,
        });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (
  (target = mod != null ? __create(__getProtoOf(mod)) : {}),
  __copyProps(
    // If the importer is in node compatibility mode or this is not an ESM
    // file that has been converted to a CommonJS file using a Babel-
    // compatible transform (i.e. "__esModule" has not been set), then set
    // "default" to the CommonJS "module.exports" for node compatibility.
    isNodeMode || !mod || !mod.__esModule
      ? __defProp(target, 'default', { value: mod, enumerable: true })
      : target,
    mod,
  )
);
var __toCommonJS = (mod) => __copyProps(__defProp({}, '__esModule', { value: true }), mod);

// src/Form/FieldList/index.tsx
var FieldList_exports = {};
__export(FieldList_exports, {
  default: () => FieldList_default,
});
module.exports = __toCommonJS(FieldList_exports);
var import_react = __toESM(require('react'));
var import_classnames = __toESM(require('classnames'));
var import_MinusCircleOutlined = __toESM(require('@ant-design/icons/MinusCircleOutlined'));
var import_PlusOutlined = __toESM(require('@ant-design/icons/PlusOutlined'));
var import_antd = require('antd');
var import_style = require('antd/es/button/style/index.js');
var import_style2 = require('antd/es/form/style/index.js');
var import_style3 = require('antd/es/message/style/index.js');
var import_FormAttrsContext = require('../../_utils/contexts/FormAttrsContext');
var import_FormItem = __toESM(require('../../FormItem'));
var import_index = require('./index.less');
function FieldList({
  when,
  name,
  label,
  max,
  min,
  hiddenTopLabel,
  className,
  style,
  header,
  footer,
  renderListItemsAddonBefore,
  renderListItemsAddon: renderListItemsAddonProp,
  renderListItemByFields,
  renderListItem: renderListItemProp,
  renderListFields,
  onAddPre,
  children,
  ...rest
}) {
  const fieldListName = name;
  const { detail } = (0, import_FormAttrsContext.useFormAttrsContext)();
  const renderListItem = (0, import_react.useCallback)(
    (listField, operators, options) => {
      const { key, name: name2, isListField } = listField;
      const { remove } = operators;
      const { listFields } = options;
      if (renderListItemProp) {
        return /* @__PURE__ */ import_react.default.createElement(
          'div',
          { key, className: 'szzj-field-list-item' },
          renderListItemProp(listField, operators, options),
        );
      }
      if (!children) return;
      const fields = /* @__PURE__ */ import_react.default.createElement(
        'div',
        { className: 'szzj-field-list-item-fields' },
        typeof children === 'function'
          ? children({ name: name2, isListField })
          : Array.isArray(children)
          ? children.map((child, index) => {
              if (typeof child === 'function') {
                const childElem = child({
                  name: name2,
                  isListField,
                  fieldListName,
                });
                return import_react.default.cloneElement(childElem, {
                  key: index,
                });
              } else {
                return import_react.default.cloneElement(child, {
                  ...child.props,
                  name: child.props.name ? [name2, child.props.name] : name2,
                  isListField,
                  key: child.props.key ?? child.props.name,
                });
              }
            })
          : import_react.default.Children.map(children, (child) => {
              return import_react.default.cloneElement(child, {
                ...child.props,
                name: child.props.name ? [name2, child.props.name] : name2,
                isListField,
                key: child.props.key ?? child.props.name,
              });
            }),
      );
      if (renderListItemByFields) {
        return /* @__PURE__ */ import_react.default.createElement(
          'div',
          { key, className: 'szzj-field-list-item' },
          renderListItemByFields(fields, {
            ...operators,
            ...options,
            listField,
          }),
        );
      }
      return /* @__PURE__ */ import_react.default.createElement(
        'div',
        { key, className: 'szzj-field-list-item' },
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          { className: 'szzj-field-list-item-header' },
          /* @__PURE__ */ import_react.default.createElement('span', null, `${label}${name2 + 1}`),
          (!min || listFields.length > min) &&
            !detail &&
            /* @__PURE__ */ import_react.default.createElement(
              'span',
              {
                className: 'szzj-field-list-remove',
                onClick: () => remove(name2),
              },
              /* @__PURE__ */ import_react.default.createElement(
                import_MinusCircleOutlined.default,
                { className: 'szzj-field-list-remove-icon' },
              ),
              '删除',
            ),
        ),
        fields,
      );
    },
    [renderListItemProp, renderListItemByFields],
  );
  const renderListItemsAddon = (0, import_react.useCallback)(
    (options) => {
      if (renderListItemsAddonProp) return renderListItemsAddonProp(options);
      const { listFields, max: max2, detail: detail2, add, errors } = options;
      if (!max2 || (listFields.length < max2 && !detail2)) {
        return /* @__PURE__ */ import_react.default.createElement(
          import_react.default.Fragment,
          null,
          /* @__PURE__ */ import_react.default.createElement(
            import_antd.Button,
            {
              type: 'dashed',
              className: 'szzj-field-list-add',
              onClick: () => {
                if (onAddPre) {
                  try {
                    const result = onAddPre({ index: listFields.length });
                    add(result);
                  } catch (err) {
                    import_antd.message.error(err.message ?? '添加失败');
                  }
                } else {
                  add();
                }
              },
              block: true,
              icon: /* @__PURE__ */ import_react.default.createElement(
                import_PlusOutlined.default,
                null,
              ),
            },
            '添加',
            label ?? '',
          ),
          /* @__PURE__ */ import_react.default.createElement(import_antd.Form.ErrorList, {
            errors,
          }),
        );
      }
      return null;
    },
    [renderListItemsAddonProp, onAddPre],
  );
  return /* @__PURE__ */ import_react.default.createElement(
    import_FormItem.default,
    {
      label,
      when,
      className: (0, import_classnames.default)('szzj-field-list-wrapper', {
        'hidden-top-label': hiddenTopLabel,
      }),
    },
    /* @__PURE__ */ import_react.default.createElement(
      'div',
      { className: (0, import_classnames.default)('szzj-field-list', className), style },
      header &&
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          { className: 'szzj-field-list-header' },
          header,
        ),
      /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.List,
        { name, ...rest },
        (listFields, { add, remove, move }, { errors, warnings }) => {
          const listFieldsNode = renderListFields
            ? renderListFields({
                name,
                listFields,
                add,
                remove,
                move,
                errors,
                warnings,
              })
            : listFields.map((listField) => {
                return renderListItem(
                  listField,
                  { add, remove, move },
                  {
                    listFields,
                    min,
                    max,
                    detail,
                  },
                );
              });
          const renderAddonOptions = {
            add,
            remove,
            move,
            listFields,
            min,
            max,
            detail,
            errors,
            warnings,
          };
          return /* @__PURE__ */ import_react.default.createElement(
            import_react.default.Fragment,
            null,
            /* @__PURE__ */ import_react.default.createElement(
              'div',
              { className: 'szzj-field-list-items-addon-before' },
              renderListItemsAddonBefore ? renderListItemsAddonBefore(renderAddonOptions) : null,
            ),
            /* @__PURE__ */ import_react.default.createElement(
              'div',
              { className: 'szzj-field-list-items' },
              listFieldsNode,
            ),
            renderListItemsAddon(renderAddonOptions),
          );
        },
      ),
      footer &&
        /* @__PURE__ */ import_react.default.createElement(
          'div',
          { className: 'szzj-field-list-footer' },
          footer,
        ),
    ),
  );
}
var FieldList_default = FieldList;
